<?php

namespace api\modules\v1\models;

use api\func\func;
use Yii;
use api\common\models\Common;
use yii\web\NotFoundHttpException;
use yii\web\HttpException;
use yii\data\ActiveDataProvider;
use api\modules\v1\models\News;
use api\common\controllers\CommonController;
use api\common\models\Mar;
use yii\data\Pagination;
use yii\web\Link;
class LookHistory extends Common
{

    /**
     * @return array|string
     */
    public static function tableName()
    {
        return '{{%look_history_app}}';
    }


    public function rules()
    {
        return [
        ];
    }

    /*
     * 添加浏览历史记录
     * @param $uid
     * @param $catid
     * @param $news_id
     * @return bool
     */
    public function add($uid,$catid,$news_id){
        if($uid !='' && $catid !='' && $news_id !=''){
            if($data = self::findOne(['catid'=>$catid,'news_id'=>$news_id,'uid'=>$uid])){
                self::updateAll(['time'=>date('Y-m-d H:i:s')],['id'=>$data->id]);
            }else{
                $count = count(self::findAll(['uid'=>$uid]));
                if($count >= 10){ //最多只能存10条记录
                    $first_id = self::find()->where(['uid'=>$uid])->orderBy('id asc')->limit(1)->one();//删除最开始的一条
                    self::deleteAll(['id'=>$first_id->id]);
                }
                $this->catid = $catid;
                $this->news_id = $news_id;
                $this->uid = $uid;
                $this->time = date('Y-m-d H:i:s');
                $this->isNewRecord = true;
                $this->save(false);
            }
        }
        return true;
    }



}
